#include "StdAfx.h"
#include "PhysX.h"
#include "PhysicsDebugRenderer.h"
#include "Level.h"
#include "float3.h"
#include "RenderContext.h"
#include "GraphCamera.h"

PhysicsDebugRenderer::PhysicsDebugRenderer(PhysX* pPhysX) :  
	m_pPhysX(pPhysX),
	m_pVertexBuffer(NULL),
	m_pVertexLayout(NULL),
	m_pWorldViewProjectionVariable(NULL),
	m_VertexBufferStride(0),
	m_vertexbuffersize(10)
{
	pPhysX->GetSDK()->setParameter(NX_VISUALIZATION_SCALE, 1.0f);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_BODY_AXES , 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_BODY_JOINT_GROUPS, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_BODY_MASS_AXES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_AABBS, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_AXES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_CCD , 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_COMPOUNDS, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_DYNAMIC, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_EDGES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_SPHERES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_COLLISION_STATIC, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_FORCE_FIELDS, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_JOINT_LOCAL_AXES, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_JOINT_LIMITS, 1);
	pPhysX->GetSDK()->setParameter(NX_VISUALIZE_WORLD_AXES, 1);
}

PhysicsDebugRenderer::~PhysicsDebugRenderer(void)
{
	if( m_pVertexLayout ) m_pVertexLayout->Release();
	if( m_pVertexBuffer ) m_pVertexBuffer->Release();
}


void PhysicsDebugRenderer::Initialize(ContentManager *pContentManager)
{
	CreateEffect( L"./Effect/physics.fx",pContentManager);
	GetEffectVariables();	
	DefineInputlayout();
	BuildVertexBuffer();
}

void PhysicsDebugRenderer::CreateEffect(wstring effect, ContentManager *pContentManager)
{
	m_pDefaultEffect = pContentManager->GetEffect(m_pLevel->GetDevice(),effect);	

	//get first technique found
	m_pTechnique = m_pDefaultEffect->GetTechniqueByIndex(0);

	GetEffectVariables();
}

void PhysicsDebugRenderer::BuildVertexBuffer()
{

}

void PhysicsDebugRenderer::GetEffectVariables()
{
	m_pTechnique = m_pDefaultEffect->GetTechniqueByName("CheckersTech");
	m_pWorldViewProjectionVariable = m_pDefaultEffect->GetVariableByName("gWVP")->AsMatrix();
}

void PhysicsDebugRenderer::DefineInputlayout()
{

}

void PhysicsDebugRenderer::UpdateEffectVariables()const
{
	
}

void PhysicsDebugRenderer::Tick(const InputState & refInputState)
{

}

void PhysicsDebugRenderer::OnResize()
{
}

void PhysicsDebugRenderer::Draw(const RenderContext* pRenderContext)
{
	D3DXMATRIX matView = pRenderContext->GetCamera()->GetView();
	D3DXMATRIX matProj = pRenderContext->GetCamera()->GetProj();
	m_pWorldViewProjectionVariable->SetMatrix((float*)&(m_World*matView*matProj));
	Draw(m_pTechnique);
}


void PhysicsDebugRenderer::Draw(const ID3D10EffectTechnique* pTechnique)
{
	// Draw the physics in wireframe
	const NxDebugRenderable *dbgRenderable=m_pPhysX->GetScene()->getDebugRenderable();    

	if (dbgRenderable == NULL)
		return;

	if(!pTechnique)
	{
		MessageBox(0,_T("No Technique"),_T("ERROR"),0);
		return;
	}

	//Every object has it's own set of eg worldmatrix
	

	// Set the input layout
	m_pLevel->GetDevice()->IASetInputLayout( m_pVertexLayout );

	DrawLines(dbgRenderable, pTechnique);           
}

void PhysicsDebugRenderer::DrawLines(const NxDebugRenderable *dbgRenderable, const ID3D10EffectTechnique* pTechnique) const
{
	
}